/* 常用的工具类函数*/

import axios from 'axios';

// 检查电话格式
function checkPhoneNum(phone) {
  phone = phone + '';
  console.log(typeof phone)
  return phone.test(/^[1-9][1-9][10]$/);
}

//公共工具方法
var Vue = require('vue');
// Vue.http.options.credentials = true;


//post请求，添加sessionId
function post(url, param, dataForm = null, callBack, failBack = null) {
  let sessionId = sessionStorage.getItem('sessionId');
  let sendUrl = url;
  for (let item in dataForm) {
    sendUrl = sendUrl + "?" + item + '=' + dataForm[item];
  }
  // console.log('post参数', param)
  Vue.default.http.post(sendUrl, param, {
    headers: {
      sessionId: sessionId,
    },
  }).then(res => {
    callBack(res.body)
  }, res => {
    if (failBack) {
      failBack(res.body)
    }
  });
}



//get请求，添加sessionId权限
function get(url, dataForm = null, param, callBack, failBack = null) {
  let sessionId = sessionStorage.getItem('sessionId');
  let sendUrl = url;
  var i = 0;
  for (let item in dataForm) {
    i++;
    if (i == 1) {
      sendUrl = sendUrl + "?" + item + '=' + dataForm[item];
    } else {
      sendUrl = sendUrl + "&" + item + '=' + dataForm[item];
    }
  }

  // Vue.default.http.get(sendUrl, {
  //   headers: {
  //     sessionId: sessionId,
  //   },
  //   emulateJSON: true,
  //   credentials: true
  // }).then(res => {
  //   callBack(res.body)
  // }, res => {
  //   if (failBack) {
  //     failBack(res.body)
  //   }
  // })

  $.ajax({
    type: 'GET',
    url: sendUrl,
    headers: {
      sessionId: sessionId,
      "Content-Type": "application/json;charset=UTF-8"
    },
    success: function (res) {
      // console.log(res);
      callBack(res)
    },
    error: function (err) {
      // console.log(err)
      failBack(err)
    }
  })
}

//axios get
// function axiosGet(url, dataForm = null, param, callBack, failBack = null) {
//   let sessionId = sessionStorage.getItem('sessionId');
//   let sendUrl = url;
//   // for (let item in dataForm) {
//   //   i++;
//   //   if (i == 1) {
//   //     sendUrl = sendUrl + "?" + item + '=' + dataForm[item];
//   //   } else {
//   //     sendUrl = sendUrl + "&" + item + '=' + dataForm[item];
//   //   }
//   // }
//   axios({
//     method: 'post',
//     url: sendUrl,
//     data: param,
//     headers: {
//       'Content-Type': 'application/json;charset=UTF-8',
//       sessionId: sessionId,
//     },
//     credentials: 'include'
//   }).then(res => {
//     callBack(res);
//   }).catch(err => {
//     failBack(err);
//   })
// }

function formSubmit(url, param, callBack, failBack = null) {
  Vue.default.http.post(url, param, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    emulateJSON: true
  }).then(res => {
    callBack(res.data)
  }, res => {
    if (failBack) {
      failBack(res.data)
    }
  });
}

function uploadFile(url, param, contentType, callBack, failBack = null) { // 上传图片到阿里云
  Vue.default.http.put(url, param, {
    headers: {
      'Content-Type': contentType
    },
    emulateJSON: false
  }).then(res => {
    callBack(res.data)
  }, res => {
    if (failBack) {
      failBack(res.data)
    }
  });
}

function setCookie(name, value) {
  var Days = 2;
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  // document.cookie = name + "=" + escape(value) ;
  document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
}

//读取cookie：
function getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null;
}

export default {
  post: post,
  get: get,
  getCookie: getCookie,
  uploadFile: uploadFile,
  formSubmit: formSubmit,
  // axiosGet: axiosGet
}
